package com.simple.elpmobile.onlinecase.engine.manager;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.simple.elpmobile.domain.CaseInfo;
import com.simple.elpmobile.domain.Doctor;
import com.simple.elpmobile.domain.Patient;
import com.simple.elpmobile.onlinecase.common.data.BaseGlobalVaribles;
import com.simple.elpmobile.onlinecase.engine.IManager;
import com.simple.elpmobile.onlinecase.engine.network.NetworkConstains;
import com.simple.elpmobile.onlinecase.engine.network.ResponseConstains;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommanHandler;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommitHandler;

/**
 * 与后台交互所有接口的提供者
 * 
 * @author jgzheng 2013-12-30
 */
public class NetworkManager implements IManager {

	private final static String TAG = "NetworkManager";

	public NetworkManager() {

	}

	@Override
	public byte managerId() {
		return NETWOTK_ID;
	}

	// 登陆功能实现
	public void login(String name, String pw, final CommanHandler handler) {
		String url = NetworkConstains.URL_HEAD + "patientlogin.jsp?nick="
				+ name + "&password=" + pw;
		new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {
					JSONObject jo = new JSONObject(content);
					if (jo != null) {
						Patient patient = new Patient();
						patient.setAge(jo.getInt("age"));
						patient.setId(jo.getInt("id"));
						patient.setName(jo.getString("name"));
						patient.setPass(jo.getString("pass"));
						patient.setSex(jo.getString("sex"));
						patient.setTel(jo.getString("tel"));
						patient.setNick(jo.getString("nick"));
						BaseGlobalVaribles.patientInfo = patient;
						handler.onLoginSuccess(patient);
					} else {
						handler.onLoginFailed("登陆失败，用户名或密码错误");
					}

				} catch (JSONException e) {
					handler.onLoginFailed("登陆失败，数据解析错误");
				}
			}

			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				handler.onLoginFailed(ResponseConstains.NETWORK_ERROR);
			}
		});
	}

	// 注册
	public void regist(String name, String pw, String tel, String age,
			String sex, String nick,final CommitHandler handler) {
		String url = NetworkConstains.URL_HEAD + "patientRegist.jsp?nick="
				+ nick + "&pass=" + pw+ "&sex=" + sex+ "&age=" + age+ "&tel=" + tel+ "&name=" + name;
		new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {
					JSONObject jo = new JSONObject(content);
					if (jo != null) {
						int code=jo.getInt("state");
						if (code==0) {
							handler.OnFail(0, "注册失败,用户名已存在");
						}else if(code==1){
							String info=jo.getString("info");
							handler.OnSucess(info);
						}
					}

				} catch (JSONException e) {
					handler.OnFail(0,"登陆失败，数据解析错误");
				}
			}

			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				handler.OnFail(0,ResponseConstains.NETWORK_ERROR);
			}
		});
	}
	
	//獲取醫生列表
	public void getDoctorInfo( final CommanHandler handler) {
		String url = NetworkConstains.URL_HEAD + "selectdoctor.jsp";
		System.out.println(url);
		new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {
					JSONArray ja=new JSONArray(content);
					List<Doctor> doctors=new ArrayList<Doctor>();
					for (int i = 0; i < ja.length(); i++) {
						JSONObject jo = (JSONObject) ja.get(i);
						if (jo != null) {
							Doctor doctor = new Doctor();
							doctor.setId(jo.getInt("id"));
							doctor.setName(jo.getString("name"));
							doctor.setAge(jo.getInt("age"));
							doctor.setInfo(jo.getString("info"));
							doctor.setTel(jo.getString("tel"));
							doctor.setMajor(jo.getString("major"));
							doctor.setSex(jo.getString("sex"));
							doctor.setPass(jo.getString("pass"));
							doctor.setNick(jo.getString("nick"));
							doctors.add(doctor);
						}
					}
					handler.onLoginSuccess(doctors);
				} catch (JSONException e) {
					handler.onLoginFailed("获取失败，数据解析错误");
				}
			}

			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				handler.onLoginFailed(ResponseConstains.NETWORK_ERROR);
			}
		});
	}
	
		//上传病例
		public void commitCaseInfo(CaseInfo caseInfo, final CommitHandler handler) {
			String url = NetworkConstains.URL_HEAD + "addcase.jsp?pId="
					+ caseInfo.getPatientId() + "&dId=" + caseInfo.getDoctorId()+ "&info=" + caseInfo.getPatientInfo();
			url = url.replaceAll(" ", "%20");
			new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					super.onSuccess(statusCode, content);
					try {
							JSONObject jo=new JSONObject(content);
							int code=jo.getInt("state");
							String info=jo.getString("info");
							if (code==1) {
								handler.OnSucess(info);
							}else{
								handler.OnFail(code, info);
							}
						
					} catch (JSONException e) {
						handler.OnFail(0,"获取失败，数据解析错误");
					}
				}

				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
					handler.OnFail(0,ResponseConstains.NETWORK_ERROR);
				}
			});
		}
		
		//获取病人病例
		public void getCaseInfo(int pid, final CommanHandler handler) {
			String url = NetworkConstains.URL_HEAD + "selectpatientcase.jsp?pid="+pid;
			System.out.println("病人病例**"+url);
			new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					super.onSuccess(statusCode, content);
					try {
						JSONArray ja=new JSONArray(content);
						List<CaseInfo> caseInfos=new ArrayList<CaseInfo>();
						for (int i = 0; i < ja.length(); i++) {
							JSONObject jo = (JSONObject) ja.get(i);
							if (jo != null) {
								CaseInfo caseInfo = new CaseInfo();
								caseInfo.setCreatTime(jo.getString("creatTime"));
								caseInfo.setDoctorId(jo.getInt("doctorId"));
								caseInfo.setId(jo.getInt("id"));
								caseInfo.setPatientId(jo.getInt("patientId"));
								caseInfo.setSolution(jo.getString("solution"));
								caseInfo.setPatientInfo(jo.getString("patientInfo"));
								caseInfo.setSolve(jo.getInt("solve"));
								caseInfos.add(caseInfo);
							}
						}
						handler.onLoginSuccess(caseInfos);
					} catch (JSONException e) {
						handler.onLoginFailed("获取失败，数据解析错误");
					}
				}

				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
					handler.onLoginFailed(ResponseConstains.NETWORK_ERROR);
				}
			});
		}
		

}